<?php
/**
 * 3D图片
 * @author cjli
 *
 */     
class Product3dAction extends HomeAction
{
	public function _initialize()
	{
		parent::_initialize();
	}

    public function show_product_list(){
        $pid = $this->_request('pid')?$this->_request('pid'):0;//穿上的商品ID
        $id = $this->_request('id')?$this->_request('id'):0;//删除搭配商品ID
        $clear= $this->_request('clear')?intval($this->_request('clear')):0;//清空搭配
        $md=$this->_request('md')?intval($this->_request('md')):0;//是否是麦迪莱登
        if($pid){
            $productInfo = ProductModel::get_product_info($pid);
        }
        if($md){
            $cookie_key="3dproduct_2174";
            $lm="m";
        }else{
            $cookie_key="3dproduct_china";
            $lm="c";
        }
        $imgHmtlRes['status']=false;
        $imgHmtlRes['amount']=0;
		if($pid&&!$productInfo){
            $imgHmtlRes['msg']='No Product';
            echo json_encode($imgHmtlRes);exit;
		}
        $post_orientation= $this->_request('post_orientation')?$this->_request('post_orientation'):1;//方向
        $imgPath= '/data/show2/';//图片存储目录
        $del_model=C('3dCollocation');
        //礼服特殊处理
        if($productInfo['model_id']==23) {
            $cat_parentid = CategoryModel::get_category_parentid($productInfo['catid']);
            if ($productInfo['catid'] == 2061 || $cat_parentid == 2061) {
                $productInfo['model_id'] = 18;
            } else if ($productInfo['catid'] == 2093 || $cat_parentid == 2093) {
                $productInfo['model_id'] = 2;
            }
        }
        $pidArr=unserialize(base64_decode(cookie($cookie_key)));
        //自动替换不可一起显示的商品
        if(isset($del_model[$productInfo['model_id']])){
            $del_arr=$del_model[$productInfo['model_id']];
            if(cookie($cookie_key)){
                foreach($pidArr as $k=>$v){
                    if(in_array($k,$del_arr)){
                       unset($pidArr[$k]);
                    }
                }

            }
        }
        if($productInfo) {
            $pidArr[$productInfo['model_id']] = $pid;
        }
        cookie($cookie_key,base64_encode(serialize($pidArr)));
        $pidArr=unserialize(base64_decode(cookie($cookie_key)));
       // print_r($pidArr);
        if ($id) {
            foreach ($pidArr as $j => $pid) {
                if ($pid == $id) {
                    unset($pidArr[$j]);
                }
            }
        }
        cookie($cookie_key,null);
        cookie($cookie_key,base64_encode(serialize($pidArr)));
        if($clear==1){
            $pidArr=array();
            cookie($cookie_key,null);
        }
        //右侧图片 总价
        $amount=0;
        $k=0;
        $imgHmtl = $this->normalProductImg($post_orientation, $imgPath,$pidArr, $lm);
        $imgHmtlRes['img'] = $imgHmtl;
        if($pidArr) {
            foreach ($pidArr as $j => $pid) {
                $product = ProductModel::get_product_info($pid);
                $right_img[$k]['id'] = $product['id'];
                $right_img[$k]['rightImg'] = $product['small_img'];
                $price = ProductModel::get_final_price($pid);
                $amount += $price;
                $k++;
            }
            $imgHmtlRes['status']=true;
            $amount=currency($amount);
            $imgHmtlRes['right']=$right_img;
            $imgHmtlRes['amount']=$amount;
        }
        $imgHmtlRes['fly_img']=$imgPath."small/".$productInfo['eng_id']."_".C('img_name')[$productInfo['model_id']].".png";
        echo json_encode($imgHmtlRes);
       // print_r ($imgHmtlRes);
    }
    public function normalProductImg($post_orientation,$imgPath,$pidArr, $lm){
        $imgHmtl = array();
        $imgHmtl[]= '<img src="'.$imgPath. $lm.$post_orientation.'.png" style="z-index: 0;" />';
       //print_r($pidArr);
       foreach($pidArr as $k=>$v){
           $productInfo= ProductModel::get_product_info($v);
           //层级
           if($k==25){//鞋子
               $level=20;
           } elseif($k==14){//衬衫
               $level=40;
           }else if($k==21||$k==26){//西裤牛仔裤
               $level=60;
           }else if($k==20||$k==27){//羊绒衫 马甲
               $level=80;
           }else if($k==1){//单西
               $level=100;
           }else if($k==22){//大衣
               $level=120;
           }
           if($k==23){
               //礼服特殊处理
               $cat_parentid = CategoryModel::get_category_parentid($productInfo['catid']);
               if($productInfo['catid'] == 2061 || $cat_parentid == 2061){
                   $k=18;
               }else if($productInfo['catid'] == 2093 || $cat_parentid == 2093){
                   $k=2;
               }
           }
           if($k==2){//两件套
               $imgHmtl[]= '<img src="'.$imgPath.$productInfo['eng_id']."/".$productInfo['eng_id']."_".C('img_name')['21']."_c".$post_orientation.'.png" style="z-index: 60;" />';
               $imgHmtl[]= '<img src="'.$imgPath.$productInfo['eng_id']."/".$productInfo['eng_id']."_".C('img_name')['1']."_c".$post_orientation.'.png" style="z-index: 100;" />';
           }elseif($k==18){//三件套
               $imgHmtl[]= '<img src="'.$imgPath.$productInfo['eng_id']."/".$productInfo['eng_id']."_".C('img_name')['21']."_c".$post_orientation.'.png" style="z-index: 60;" />';
               $imgHmtl[]= '<img src="'.$imgPath.$productInfo['eng_id']."/".$productInfo['eng_id']."_".C('img_name')['20']."_c".$post_orientation.'.png" style="z-index: 80;" />';
               $imgHmtl[]= '<img src="'.$imgPath.$productInfo['eng_id']."/".$productInfo['eng_id']."_".C('img_name')['1']."_c".$post_orientation.'.png" style="z-index: 100;" />';
           }else{
               $imgHmtl[]= '<img src="'.$imgPath.$productInfo['eng_id']."/".$productInfo['eng_id']."_".C('img_name')[$k]."_c".$post_orientation.'.png" style="z-index: '.$level.';" />';
           }
       }
        return $imgHmtl;
    }
}